<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-02 19:42:06
 * @LastEditTime: 2023-10-29 15:00:17
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Repositories;

use App\Models\ProProcessesInspect;
use App\Models\ProProcessesInspectPack;

/**
 * Summary of ProProcessesInspectRepository
 * @property ProProcessesPassRepository $pass
 * @property ProProcessesInspectPack $pack
 * @author sunkaiyuan
 */
class ProProcessesInspectRepository extends Repositories
    {
    public $relation_models = ["status", "creator", "location", "material", "company", "process", 'order', 'open_card'];

    public function boot(ProProcessesInspect $model, ProProcessesInspectPack $pack)
        {
        $this->model = $model;
        $this->pack  = $pack;
        }


    public function stockIn($v)
        {
        $route = $v->open_card->main->process_route->details->last();
        if ($v->status_id != 0) {
            fail("操作失败！单据状态{$v->status->name}");
            }
        // if ($v->process_id != $route->process_id) {
        //     fail("工艺路线未完成！无法入库！");
        //     }
        $iscustomer = 1;
        $customer   = $v->open_card->order?->main->customer_id;

        $formatter = $v->open_card->order->main?->check_formatter_id ?? 3;

        if ($v->pack()->count() == 0) {
            $data = [
                'auxiliary_quantity' => 1,
                'grade'              => "A",
                'hanging_code'       => 100,
                'location_id'        => "3255",
                'quantity'           => $v->open_quantity,
                'reel'               => 1,
                'unit_id'            => 3,
                'inspect_id'         => $v->id
            ];
            $this->pack->create($data);
            $v->quantity           = $v->open_quantity;
            $v->auxiliary_quantity = 1;
            }

        $v->pack->filter(function ($vv) use ($v, $customer, $iscustomer, $formatter)
            {
            $vv->mergeWhere([
                'iscustomer'  => $iscustomer,
                'customer_id' => $customer,
                'company_id'  => $customer,
                'batchno'     => $v->batchno,
                'color_name'  => $v->color_name ?? "",
                'dyelot'      => $v->dyelot ?? "",
                'style'       => $vv->style ?? "",
                'material_id' => $v->material_id,
                'orderno'     => $v->cust_order ?? "",
                'custno'      => $v->open_card->custno,
                'unit_id'     => $vv->unit_id,
                'craft'       => $v->open_card->craft_remark,
                'sample_data' => $v->open_card->is_sample == 1 ? "小样" : "大货",
            ])->addStorage('检验入库', $v->code, $vv->quantity, $vv->auxiliary_quantity);
            });
        //已入库的，没有次品的清除WIP库存
        $v->open_card->faultle_quantity += $v->quantity;
        $v->open_card->save();
        ProProcessesWipRepository::run("subWipStorage", [$v->wip_id, $v->open_quantity]);
        return $v;
        }
    public function noStockIn($item)
        {
        if ($item->status_id != 1) {
            fail("操作失败！单据状态{$item->status->name}");
            }
        ProProcessesWipRepository::run("restoreWip", [$item->wip_id, $item->open_quantity]);
        $item->pack->filter(fn($v) => $v->noaddStorage());
        }
    }